File: rtiostream_utils.c

    1   /* Copyright 2012-2013 The MathWorks, Inc. */
    2   
    3   #include "rtiostream_utils.h"
    4   
    5   /* include rtIOStream interface to use */
    6   #include "rtiostream.h" 
    7   
    8   /* TARGET_CONNECTIVITY_TESTING might be defined by some MathWorks tests for
    9    * testing purposes only. In this case, we force SIZE_MAX to be 4 so we 
   10    * could test the pointer arithmetic in rtIOStreamBlockingSend and
   11    * rtIOStreamBlockingRecv.
   12    */ 
   13   #ifdef TARGET_CONNECTIVITY_TESTING
   14       #define SIZE_MAX 4
   15   #else
   16       /* define SIZE_MAX if not already defined (e.g. by a C99 compiler) */
   17       #ifndef SIZE_MAX
   18           #define SIZE_MAX ((size_t)-1)
   19       #endif
   20   #endif
   21   
   22   #ifndef MemUnit_T
   23      /* External Mode */
   24      typedef unsigned char IOUnit_T; 
   25   #else
   26      /* SIL/PIL */
   27      #ifdef HOST_WORD_ADDRESSABLE_TESTING
   28         /* rtIOStream will handle data in single byte chunks 
   29          *
   30          * uint8_T can be > 8-bits for certain portable word sizes 
   31          * cases (e.g. C2000) so use native type instead */
   32         typedef unsigned char IOUnit_T;
   33      #else
   34         /* rtIOStream will handle data in MemUnit_T size chunks */
   35         typedef MemUnit_T IOUnit_T;
   36      #endif
   37   #endif
   38   
   39   /* Blocks until all requested outgoing data is sent */
   40   int rtIOStreamBlockingSend(const int streamID, 
   41                              const void * const src, 
   42                              uint32_T size) {
   43   
   44       size_t transferAmount;
   45       size_t sizeSent;
   46       int errorCode      = RTIOSTREAM_NO_ERROR;   
   47       const IOUnit_T * srcPtr = (const IOUnit_T *) src;
   48       
   49       /* use a variable to avoid SIZE_MAX being treated as a constant
   50        * which leads to compiler warnings for "MIN" on platforms where
   51        * SIZE_MAX > UINT32_MAX */
   52       size_t sizeMax = SIZE_MAX;
   53       while (size > 0) {
   54           /* support full uint32 size */
   55           transferAmount = (size_t) MIN(sizeMax, size);        
   56           errorCode = rtIOStreamSend(streamID,
   57                                      (const void *) srcPtr,
   58                                      transferAmount,
   59                                      &sizeSent);
   60           if (errorCode == RTIOSTREAM_ERROR) {
   61               return errorCode;
   62           }
   63           else {            
   64               size -= (uint32_T) sizeSent;
   65               srcPtr += sizeSent;
   66           }
   67       }
   68       return errorCode;
   69   }
   70   
   71   /* Blocks until all requested incoming data is received */
   72   int rtIOStreamBlockingRecv(const int streamID,
   73                              void * const dst,
   74                              uint32_T size) {
   75   
   76      size_t transferAmount;
   77      size_t sizeRecvd;
   78      int errorCode      = RTIOSTREAM_NO_ERROR;
   79      IOUnit_T * dstPtr = (IOUnit_T *) dst;
   80      
   81      /* use a variable to avoid SIZE_MAX being treated as a constant
   82       * which leads to compiler warnings for "MIN" on platforms where 
   83       * SIZE_MAX > UINT32_MAX */
   84      size_t sizeMax = SIZE_MAX;
   85      while (size > 0) {
   86         /* support full uint32 size */
   87         transferAmount = (size_t) MIN(sizeMax, size);      
   88         errorCode = rtIOStreamRecv(streamID, 
   89                                    (void *) dstPtr, 
   90                                    transferAmount, 
   91                                    &sizeRecvd);
   92         if (errorCode == RTIOSTREAM_ERROR) {
   93               return errorCode;
   94         }
   95         else {          
   96            size -= (uint32_T) sizeRecvd;
   97            dstPtr += sizeRecvd;
   98         }
   99      }
  100      return errorCode;
  101   }
  102   
  103   
  104